<template>
  <div>
    <breadcrumb>
      <span slot="first">首页</span>
      <span slot="second">商品管理</span>
      <span slot="third">商品分类</span>
    </breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCategory">添加分类</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsCateList" row-key="cat_id" :tree-props="{children: 'children', hasChildren: true}">
        <el-table-column prop="cat_name" label="分类名称" width="300px">
        </el-table-column>
        <el-table-column label="是否有效" width="300px">
          <template slot-scope="scope">
            <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color: lightgreen; font-size: 20px"></i>
            <i class="el-icon-error" v-else style="color: red; font-size: 20x"></i>
            </template>
          </el-table-column>
          <el-table-column label=" 排序" width="300px">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
                <el-tag v-else-if="scope.row.cat_level === 1" type="warning">二级</el-tag>
                <el-tag v-else type="danger">三级</el-tag>
              </template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" class="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" class="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
        :page-sizes="[1,5,10,20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog @close="addCategoryClose" title="添加分类" :visible.sync="addCategorydialogVisible" width="50%">
      <span>
        <el-form ref="addCategoryRef" :rules="addCategoryRules" :model="addCategoryForm" label-width="80px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCategoryForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader @change="cascaderChange" clearable style="width: 100%" v-model="cascaderValue"
              :options="cascaderKeys" :props="cascaderProps">
            </el-cascader>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCategorydialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from "@/components/common/Breadcrumb"
import { request } from "@/network/request.js"
export default {
  name: "Cate",
  components: {
    Breadcrumb
  },
  data() {
    return {
      // 商品分类数据列表
      goodsCateList: [],
      // 查询参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类数据长度
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          width: '400px',
        },
      ],
      // 显示添加用户对话框
      addCategorydialogVisible: false,
      // 添加用户表单验证规则
      addCategoryRules: {
        cat_name: {
          required: true, message: '请输入分类名称', trigger: 'blur'
        },
      },
      // 添加用户表单数据对象
      addCategoryForm: {
        cat_name: '',
        // 分类父id
        cat_pid: 0,
        // 分类层级：一级/二级/三级 
        cat_level: 0

      },
      // 级联绑定数据
      cascaderValue: [],
      // 级联数据源
      cascaderKeys: [],
      // 级联props选项
      cascaderProps: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true
      }
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await request({
        url: "/categories",
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return
      // console.log(res);
      this.total = res.data.total
      this.goodsCateList = res.data.result
    },
    async getCascadeData() {
      // 获取级联框中所有一级二级分类的数据
      const { data: res } = await request({
        url: "/categories",
        params: { type: 2 }
      })
      if (res.meta.status !== 200) return
      this.cascaderKeys = res.data
    },
    showAddCategory() {
      this.getCascadeData()

      this.addCategorydialogVisible = true
    },
    // 级联选项发送变化
    cascaderChange() {
      // console.log(this.cascaderValue);
      if (this.cascaderValue.length !== 0) {
        this.addCategoryForm.cat_pid = this.cascaderValue[this.cascaderValue.length - 1]
        this.addCategoryForm.cat_level = this.cascaderValue.length
      } else {
        this.addCategoryForm.cat_pid = 0
        this.addCategoryForm.cat_level = 0
      }
    },
    // 添加分类确定按钮
    handleCategory() {
      console.log(this.addCategoryForm);
      // 进行表单预验证
      this.$refs.addCategoryRef.validate(async valid => {
        if (!valid) return
        // 发送网络请求
        const { data: res } = await request({
          method: "post",
          url: "/categories",
          data: this.addCategoryForm
        })
        if (res.meta.status !== 201) return
        this.$message.success("添加分类成功")
        this.getGoodsList()
        this.addCategorydialogVisible = false
      })
    },
    // 表单关闭，重置表单数据
    addCategoryClose() {
      this.$refs.addCategoryRef.resetFields()
      // 对级联框进行重置
      this.cascaderValue = []
      this.addCategoryForm.cat_level = 0
      this.addCategoryForm.cat_pid = 0
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    } 
  },
  created() {
    this.getGoodsList()
  },
}
</script>

<style lang='less' scoped>

</style>